﻿using MSSClient.Model;
using MSSClient.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows;

namespace MSSClient
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class UpgradeClientDlg : Window
    {
        public delegate void DownLoadDelegate(string pram);
        public DownLoadDelegate downLoadDelegate;
        private delegate void DelegateFunction(int ipos);
        public string tag = "0";//下载成功的标识，0-失败，1-成功
        public UpgradeClientDlg()
        {
            InitializeComponent();
            string filePath = @"C:\MssClientUpGrade\";
            if (!Directory.Exists(filePath))//判断文件夹是否存在 filePath为文件保存的路径
                Directory.CreateDirectory(filePath);//不存在则创建文件夹
            File.Delete("C:/MssClientUpGrade/setup.exe");

            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = "C:/MssClientUpGrade/";
            watcher.Created += new FileSystemEventHandler(UpgradeFileCreate);
            watcher.EnableRaisingEvents = true;

            Thread multi = new Thread(new ThreadStart(DownLoadFile));
            multi.IsBackground = true;
            multi.Start();
            multi.Priority = ThreadPriority.Highest;
            Console.WriteLine("下载线程名称："+multi.Name);

            Thread multi1 = new Thread(new ThreadStart(StartWork));
            multi1.IsBackground = true;
            multi1.Start();
            Console.WriteLine("下载UI线程名称：" + multi1.Name);
        }

        void DownLoadFile()
        {
            Dictionary<string, string> paramsDic2 = new Dictionary<string, string>();
            paramsDic2.Add("account", "rr");
            paramsDic2.Add("password", "rr");
            paramsDic2.Add("key", "key");
            tag = CommonUtil.clientPost(Global.getInstance().strUrl + Global.getInstance().strgetCLient, paramsDic2);
            label1.Dispatcher.Invoke(new Action(() =>
            {
                label1.Content = "下载完成，马上进行安装";
            }));
        }

        void StartWork()
        {
            //设置进度条最大值 
            //this.progressBar.Maximum = 100;
            Random r2 = new Random();
            int t = r2.Next(90);
            for (int i = 1; i <= 98; i++)
            {
                label1.Dispatcher.Invoke(new Action(() =>
                {
                    if (label1.Content.Equals("下载完成，马上进行安装"))
                    {
                        i = 100;
                        SetPos(100);
                    }
                }));
                if (i==t)
                    System.Threading.Thread.Sleep(4000);
                if (i == 96)
                {
                    progressBar.Dispatcher.Invoke(new Action(() =>
                    {
                        progressBar.Value = i;
                    }));
                    label2.Dispatcher.Invoke(new Action(() =>
                    {
                        label2.Content = i + "%(目前服务器响应较慢，请耐心等待...)";
                    }));
                    System.Threading.Thread.Sleep(50000);
                }

                int v = r2.Next(900);
                System.Threading.Thread.Sleep(v);
                SetPos(i);
            }
        }

        public void UpgradeFileCreate(object source, FileSystemEventArgs e)
        {
            System.Threading.Thread.Sleep(1500);
            Process p = Process.Start("C:/MssClientUpGrade/setup.exe");
            this.Dispatcher.Invoke(new Action(() =>
            {
                this.Close();
            }));
        }
        //设置进度条的Value
        private void SetPos(int ipos)
        {
            progressBar.Dispatcher.Invoke(new Action(() =>
            {
                progressBar.Value = ipos;
            }));
            label2.Dispatcher.Invoke(new Action(() =>
            {
                label2.Content = ipos+"%";
            }));
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Process p = Process.Start("C:/MssClientUpGrade/setup.exe");
            this.Close();
        }
    }
}
